package com.ease.framework.gef4g.actions {
   import com.ease.framework.assets.icons.IconsConstant;
   import com.ease.framework.gef.events.GraphicalZoomEvent;
   import com.ease.framework.gef4g.IGraphicalEditor;
   import com.ease.framework.workbench.Workbench;

   import flash.events.KeyboardEvent;
   import flash.events.MouseEvent;
   import flash.ui.Keyboard;

   import mx.containers.HBox;
   import mx.controls.LinkButton;
   import mx.controls.Spacer;
   import mx.controls.TextInput;
   import mx.core.Application;
   import mx.core.FlexGlobals;

   public class ZoomButton extends HBox {
      [Bindable]
      public var ZoomNumMin:Number = .25;
      [Bindable]
      public var ZoomNumMax:Number = 3;

      private var currentZoomNum:Number = 1.0;

      private var button_zoomin:LinkButton = new LinkButton();
      private var button_zoomout:LinkButton = new LinkButton();
      private var input_zoom:TextInput = new TextInput();

      public function ZoomButton() {
         super();
         this.setStyle("horizontalGap", 1);

         button_zoomin.setStyle("icon", IconsConstant.ZOOM_IN);
         button_zoomout.setStyle("icon", IconsConstant.ZOOM_OUT);

         button_zoomin.width = 30;
         button_zoomout.width = 30;
         input_zoom.width = 40;
         input_zoom.maxWidth = 40;


         this.addChild(button_zoomin);

         this.addChild(button_zoomout);
         this.addChild(new Spacer());
         this.addChild(input_zoom);

         button_zoomin.addEventListener(MouseEvent.CLICK, onMouseClick_zoomin);
         button_zoomout.addEventListener(MouseEvent.CLICK, onMouseClick_zoomout);

         input_zoom.text = "" + (int(100 * currentZoomNum)) + "%";
         input_zoom.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

         FlexGlobals.topLevelApplication.addEventListener(GraphicalZoomEvent.ZOOM_CHANGED, onZoomChanged);
      }

      protected function onMouseClick_zoomin(event:MouseEvent):void {
         zoom("in");
      }

      protected function onMouseClick_zoomout(event:MouseEvent):void {
         zoom("out");
      }

      protected function onZoomChanged(event:GraphicalZoomEvent):void {
         currentZoomNum = IGraphicalEditor(Workbench.instance.currentEditor).getGraphicalViewer().getZoomCurrentNumber();
         input_zoom.text = "" + (int(100 * currentZoomNum)) + "%";
      }

      private function zoom(direction:String, zoomNum:Number = 0.0):void {
         if (zoomNum != 0.0) {
            currentZoomNum = zoomNum;
         }
         else {
            switch (direction) {
               case "in":
                  if (currentZoomNum + 0.2 > ZoomNumMax) {
                     currentZoomNum = ZoomNumMax;
                  }
                  else {
                     currentZoomNum = currentZoomNum + 0.2;
                  }
                  if (currentZoomNum > 0.9 && currentZoomNum < 1.1) {
                     currentZoomNum = 1.0;
                  }
                  break;
               case "out":
                  if (currentZoomNum - 0.2 < ZoomNumMin) {
                     currentZoomNum = ZoomNumMin;
                  }
                  else {
                     currentZoomNum = currentZoomNum - 0.2;
                  }
                  if (currentZoomNum > 0.9 && currentZoomNum < 1.1) {
                     currentZoomNum = 1.0;
                  }
                  break;
            }
         }

         input_zoom.text = "" + (int(100 * currentZoomNum)) + "%";

         IGraphicalEditor(Workbench.instance.currentEditor).getGraphicalViewer().doZoom(currentZoomNum);
      }

      protected function onKeyDown(event:KeyboardEvent):void {
         event.stopImmediatePropagation(); // stop the main list from using alphabnumeric navigation

         if (event.keyCode == Keyboard.ENTER) {
            var text:String = input_zoom.text;
            text = text.split("%")[0];
            var tmpN:Number = new Number(text);
            if (isNaN(tmpN))
               tmpN = 100;
            tmpN = tmpN / 100;
            zoom("", tmpN);
         }
      }
   }
}